<?php
class erpapi_shop_request_logistics extends erpapi_shop_request_abstract
{
    public function searchAddress($rdef)
    {
        $shop_id = $this->__channelObj->channel['shop_id'];
        $data = array('shop_id' => $this->__channelObj->channel['shop_id'],'shop_type'=> $this->__channelObj->channel['shop_type'],'obj_bn'=>$this->__channelObj->channel['shop_id']);
        $params = array(
            'search_type'=>$rdef,    
        );
        
        $callback = array(
            'class' => get_class($this),
            'method' => 'searchAddress_callback',
            'params' => $data
        );
        $title = '店铺('.$this->__channelObj->channel['name'].')获取地址库列表';

        $this->__caller->call(SHOP_LOGISTICS_ADDRESS_SEARCH,$params,$callback,$title,10,$this->__channelObj->channel['shop_id']);
    }


    public function searchAddress_callback($response, $callback_params){
        
        $oAddress = app::get('ome')->model('return_address');
        $shop_id = $callback_params['shop_id'];
        $rsp = $response;
        if ($rsp['rsp']=='succ' && $shop_id) {
            $address_list = json_decode($rsp['data'],true);
             $address_list= $address_list['address_result'];
            //保存至本地
            if ($address_list) {
                foreach ($address_list as $list ) {
                    $data = array(
                        'cancel_def'    =>$list['cancel_def'],
                        'city'          =>$list['city'],
                        'area_id'       =>$list['area_id'],
                        'phone'         =>$list['phone'],
                        'mobile_phone'  =>$list['mobile_phone'],
                        'province'      =>$list['province'],
                        'addr'          =>$list['addr'],
                        'country'       =>$list['country'],
                        'contact_id'    =>$list['contact_id'],
                        'get_def'       =>$list['get_def'],
                        'contact_name'  =>$list['contact_name'],
                        'seller_company'=>$list['seller_company'],
                        'send_def'      =>$list['send_def'],
                        'zip_code'      =>$list['zip_code'],
                        'shop_type'     =>$callback_params['shop_type'],
                        'shop_id'       =>$shop_id,
                    );
                    $re = $oAddress->db->selectrow('SELECT count(1) AS num FROM sdb_ome_return_address WHERE shop_id="'.$shop_id.'" and area_id="'.$list['area_id'].'"'); 
                    $filter = array(
                            'shop_id'=>$shop_id,
                            'area_id'=>$list['area_id']
                    );
                    if($re['num']<=0){
                        $rp = $oAddress->save($data);  
                    }   
                    else{
                        //$rp = $oAddress->update($data,$filter);
                        $rp = $oAddress->delete($filter);  
                        $rp = $oAddress->save($data);  
                    }
                    
                }
            }
            
        } 
        return $this->callback($response, $callback_params);    
    }

    public function updateReturnLogistics($reshipinfo) {}

    /**
     * 批量获取云栈大头笔信息
     * @param   array  $datas    发货单信息
     * @param   string $cp_code  物流公司编号
     * @return  object $res      返回信息结果
     * @access  public
     * @author  liuzecheng@shopex.cn
     */
    public function getCloudStackPrintTags($datas,$cp_code) {
        $params = array(
            'cp_code' => $cp_code,
        );
        foreach ($datas as $data) {
            $address_pairs = array(
                'shipping_address'=>array(
                    'area'=>$data['dly_area_2'],
                    'province'=>$data['dly_area_0'],
                    'town'=>'',
                    'city'=>$data['dly_area_1'],
                    'address_detail'=>$data['dly_address'],
                ),
                //订单号非必须参数
                'trade_order_code'=> $data['delivery_bn'],
                //收货人地址
                'consignee_address'=> array(
                    'area'=>$data['ship_area_2'],
                    'province'=>$data['ship_area_0'],
                    'town'=> '',
                    'city'=>$data['ship_area_1'],
                    'address_detail'=> $data['ship_addr'],
                ),
                //物流单号非必须参数（暂时屏蔽）
                // 'waybill_code'=> ''
            );
            $address_pairss[] = $address_pairs;
        }
        $params['address_pairs'] = json_encode($address_pairss);
        $title = '获取（' . $cp_code . '）的' . '云栈大头笔';
        // 记录获取云栈大头笔日志
        $result = $this->__caller->call(SHOP_GET_CLOUD_STACK_PRINT_TAG, $params, array(), $title, 20);
        return $result;
    }
}